//
//  RACMulticastConnectionVC.m
//  ReactiveCocoa
//
//  Created by apple on 2017/9/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "RACMulticastConnectionVC.h"
#import <ReactiveObjC/ReactiveObjC.h>
@interface RACMulticastConnectionVC ()

@end

@implementation RACMulticastConnectionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
#pragma RACMulticastConnection - 多次订阅信号的时候，防止多次发送请求 - 再次订阅信号的时候，只会发送已经请求到的数据，不会再次请求
    
    // 1.创建信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
       
        // connect 的时候才会来到这个block
        NSLog(@"发送请求");
        
        // 3.发送信号
        [subscriber sendNext:@"烯烯"];
        
        return nil;
    }];
    
    
    RACMulticastConnection *connection = [signal publish];
    
    // 2.订阅信号
    [connection.signal subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"signal第一次 = %@",x);
    }];
    
    // 2.再次订阅信号
    [connection.signal subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"signal第二次 = %@",x);
    }];
    
    // 4.连接
    [connection connect];
    

//    [self requestBug];
}


#pragma 旧方法 - 每订阅一次信号 - 就会请求一次数据
- (void)requestBug{

    // 1.创建信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        NSLog(@"发送请求");
        
        // 3.发送信号
        [subscriber sendNext:@"烯烯"];
        
        return nil;
    }];
    
    // 2.订阅信号
    [signal subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"旧方法signal第一次 = %@",x);
    }];
    
    // 2.再次订阅信号
    [signal subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"旧方法signal第一次 = %@",x);
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
